package cn.com.nes.site.module.service;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.ParameterUtil;
import cn.com.nes.mybatis.agent.contract.entity.ContractInfo;
import cn.com.nes.mybatis.agent.contract.mapper.ContractInfoMapper;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.ProjectDaoImpl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@Service
public class ProjectService {
	@Resource
	private ProjectDaoImpl impl;

	@Resource
	private ContractInfoMapper contractInfoMapper;
	public PageData addProject(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ;
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.addProject(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
		
	}
	
	public PageData addProjectScheduling(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020302") ;
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			
			res = impl.addProjectScheduling(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	
	
	public PageData getSchedulingList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getSchedulingList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	public PageData getProjectHomePage(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProjectHomePage(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getProgressOverview(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProgressOverview(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData deleteData(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //项目
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.deleteData(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	
	public PageData addProjectCheck(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020602") ; //安全检查
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.addProjectCheck(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
			
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	public PageData getBulidList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getBulidList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData addBulidLog(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //项目登记
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			
			res = impl.addBulidLog(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	public PageData updateProjectAuditStatus(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020601") ; //项目验收
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.updateProjectAuditStatus(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	
	public PageData sendProjectOvertimeRemind(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.sendProjectOvertimeRemind(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getProjectList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProjectList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getAllocatedList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getAllocatedList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getInstallUsers(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getInstallUsers(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData addWorkOrder(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020401") ; //工单信息
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			
			res = impl.addWorkOrder(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	
	public PageData changeOrderStatus(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020401") ; //工单信息
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.changeOrderStatus(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	
	public PageData getWorkOrderList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getWorkOrderList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	public PageData getOpportunityList(DataReq req) throws SQLException  {
		PageData res = new PageData() ;
		String name = ParameterUtil.getString(req, "opportunityname");
		String startDate = ParameterUtil.getString(req, "start_date");
		String endDate = ParameterUtil.getString(req, "end_date");

		QueryWrapper<ContractInfo> wrapper = new QueryWrapper<>();
		wrapper.select("id as opportunity_uuid","name as opportunityname", "contract_number as opportunitytype", "opportunity_id as businessId")
				.eq(ContractInfo.COMPANY_ID, req.getUinfo().getCompanyId())
				.eq(ContractInfo.DELETED, 0);
		if (StrUtil.isNotBlank(name)) {
			wrapper.like(ContractInfo.NAME, "%" + name + "%");
		}
		if (StrUtil.isNotBlank(startDate)) {
			wrapper.ge(ContractInfo.GMT_CREATE, startDate);
		}
		if (StrUtil.isNotBlank(endDate)) {
			wrapper.le(ContractInfo.GMT_CREATE, endDate);
		}
		wrapper.orderByDesc(ContractInfo.GMT_CREATE);
		wrapper.last("limit 100");
		final List<Map<String, Object>> contractInfos = contractInfoMapper.selectMaps(wrapper);

		res.setPage(contractInfos);
		res.setRetCode(0);
		res.setRetMessage("OK");
		return res ;
	}
	
	public PageData getProjectCheckList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProjectCheckList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getLogList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getLogList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	public PageData getVisualization(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getVisualization(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getWorkOrderRecordList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getWorkOrderRecordList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	
	public PageData getProjectfileList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProjectfileList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	
	public PageData saveProjectFile(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //有项目权限
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.saveProjectFile(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	
	public PageData delProjectFile(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //有项目权限
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			
			res = impl.delProjectFile(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	
	public PageData addOpeDev(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //有项目权限
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			
			res = impl.addOpeDev(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData getWeather(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn = DBUtils.getDBConn_Weather();
			res = impl.getWeather(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData getPreWeatherList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn = DBUtils.getDBConn_Weather();
			res = impl.getPreWeatherList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData userSign(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.userSign(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	public PageData getSignList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getSignList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData getSignObjectList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getSignObjectList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	
	public PageData projectReport(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.projectReport(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	
	public PageData getProjectReportList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getProjectReportList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData getPointData(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getPointData(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData getLevelParam(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getLevelParam(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData addPointsRule(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.addPointsRule(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	
	public PageData getRuleList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getRuleList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	
	public PageData getPointList(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.getPointList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
    }
	public PageData audituserPoint(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.audituserPoint(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	public PageData addUserPosition(DataReq req) throws SQLException {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.addUserPosition(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
    }
	public PageData saveProjectReportShzt(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			res = impl.saveProjectReportShzt(conn,req);
			if(res.getRetCode()==0) {
				conn.commit();
			}
		}catch (Exception e) {
		}finally {
			if(conn!=null) {
				conn.rollback();
				conn.close();
			} 
		}
		return res ;
	}
	public PageData getDevList(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn = DBUtils.getDBConn_Ope();
			res = impl.getDevList(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
	public PageData installDev(DataReq req) throws SQLException  {
		Connection conn = null ;
		PageData res = new PageData() ;
		try {
			conn =DBUtils.getJNDIDBConn() ;
			
			res = DBUtils.checkPower(conn, req,"020301") ; //有项目权限
			if (res.getRetCode()!=0) {
				return res ;
			}
			
			res = impl.installDev(conn,req);
		}catch (Exception e) {
		}finally {
			if(conn!=null) conn.close();
		}
		return res ;
	}
}
